const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const _ = db.command
// 创建集合云函数入口函数
exports.main = async (event, context) => {
  try {
    console.log('进入了counterList')
    //  num=${this.data.num}&group=${this.data.group}&redGroup='男'&blueGroup='女'
    let num = parseInt(event.num)
    let group = event.group
    let redGroup = event.redGroup
    let blueGroup = event.blueGroup
    let red = []
    let blue = []
    let randoms = []
    const students = await db.collection('student')
    //let student = await students.field({ _id: true }).get()
    let random
    //随机获取红队名单
      //获取随机_id数组
    if (group == 'group') {
      let redStudents = await students.where({ group: redGroup }).get()
      //redStudents = redStudents.data
      let redCount = redStudents.data.length
      while (true) {
        random = Math.floor(Math.random() * redCount)
        if (!randoms.includes(random)) {
          if (randoms.push(random) == num) {
            break
          }
        }
      }
      for (let index = 0; index < randoms.length; index++) {
        let id = redStudents.data[randoms[index]]._id
        red[index] = await students.doc(id).get()
        red[index] = red[index]['data']
      }
      //获取蓝队名单
      let blueStudents = await students.where({ group: blueGroup }).get()
      //redStudents = redStudents.data
      let blueCount = blueStudents.data.length
      randoms = []
      while (true) {
        random = Math.floor(Math.random() * blueCount)
        if (!randoms.includes(random)) {
          if (randoms.push(random) == num) {
            break
          }
        }
      }
      for (let index = 0; index < randoms.length; index++) {
        let id = blueStudents.data[randoms[index]]._id
        blue[index] = await students.doc(id).get()
        blue[index] = blue[index]['data']
      }
    } else if (group == 'gender') {
      let redStudents = await students.where({ gender: redGroup }).get()
      //redStudents = redStudents.data
      console.log(redStudents)
      let redCount = redStudents.data.length
      while (true) {
        random = Math.floor(Math.random() * redCount)
        if (!randoms.includes(random)) {
          if (randoms.push(random) == num) {
            break
          }
        }
      }
      for (let index = 0; index < randoms.length; index++) {
        let id = redStudents.data[randoms[index]]._id
        red[index] = await students.doc(id).get()
        red[index] = red[index]['data']
      }
      //获取蓝队名单
      let blueStudents = await students.where({ gender: blueGroup }).get()
      //redStudents = redStudents.data
      let blueCount = blueStudents.data.length
      randoms = []
      while (true) {
        random = Math.floor(Math.random() * blueCount)
        if (!randoms.includes(random)) {
          if (randoms.push(random) == num) {
            break
          }
        }
      }
      for (let index = 0; index < randoms.length; index++) {
        let id = blueStudents.data[randoms[index]]._id
        blue[index] = await students.doc(id).get()
        blue[index] = blue[index]['data']
      }
    }
    return {
      success: true,
      data: {
        student: {
          red: red,
          blue: blue
        }
      }
    }
  } catch (e) {
    console.log(e)
    return {
      success: false,
      data: e
    }
  }
}